package com.caimiao.miao.library.utils;

import android.text.TextUtils;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class DecimalUtil {
    private static final String qppHexStr = "0123456789ABCDEF";

    private static byte charToByte(char c) {
        return (byte) qppHexStr.indexOf(c);
    }

    /**
     * 十六进制的字符串转byte[]
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStr2Bytes(String hexString) {
        if (hexString == null || hexString.isEmpty()) {
            return null;
        }
        Log.e("UTIL", "CODE: " + hexString);
        hexString = hexString.toUpperCase().replace(" ", "");

        int length = hexString.length() >> 1;
        char[] hexChars = hexString.toCharArray();

        int i = 0;
        Log.i("QnDbg", "hexString.length() : " + hexString.length());
        //去除qppHexStr里没有的字符，例如空格、GHIJK等
        do {
            int checkChar = qppHexStr.indexOf(hexChars[i]);

            if (checkChar == -1)
                return null;
            i++;
        } while (i < hexString.length());
        //十六进制字符串转byte[]
        byte[] dataArr = new byte[length];

        for (i = 0; i < length; i++) {
            int strPos = i * 2;
            dataArr[i] = (byte) (charToByte(hexChars[strPos]) << 4 | charToByte(hexChars[strPos + 1]));
        }

        return dataArr;
    }


    /**
     * byte[]转为十六进制字符串
     *
     * @param dataBytes
     * @return
     */
    public static String bytesToHex(final byte[] dataBytes) {
        char temp;
        String str = "";
        for (int n = 0; n < dataBytes.length; n++) {
            temp = (char) ((dataBytes[n] & 0xf0) >> 4);
            str += (char) (temp >= 10 ? 'A' + (temp - 10) : '0' + temp);
            temp = (char) ((dataBytes[n] & 0x0f) >> 0);
            str += (char) (temp >= 10 ? 'A' + (temp - 10) : '0' + temp);
            str += ' ';
        }
        return str;
    }

    /**
     * 十六进制的字符串字符集转为十进制的字符集
     *
     * @param dat
     * @return
     */
    public static byte[] hexToChar(final byte[] dat) {
        byte[] new_byte = new byte[dat.length];
        int step = 0, index = 0;
        char ch = 0, ch1 = 0;
        for (int n = 0; n < dat.length; n++) {
            switch (step) {
                case 0:
                    if ((ch = (char) dat[n]) != ' ') {
                        step = 1;
                    }
                    break;
                case 1:
                    if ((ch1 = (char) dat[n]) == ' ') {
                        new_byte[index++] = (byte) (ch - '0');
                    } else {
                        ch = (char) (ch <= '9' ? (ch - '0') << 4 : ((ch - 'A') + 10) << 4);
                        ch1 = (char) (ch1 <= '9' ? (ch1 - '0') << 0 : ((ch1 - 'A') + 10) << 0);
                        new_byte[index++] = (byte) (ch | ch1);
                    }
                    step = 0;
                    break;
            }

        }
        byte[] sendBytes = new byte[index];
        System.arraycopy(new_byte, 0, sendBytes, 0, index);
        new_byte = null;
        return sendBytes;
    }

    /**
     * 十进制转十六进制
     *
     * @param res
     * @return
     */
    public static String Dic2Hex(int res) {
        String tmp = "";
        if (res / 16 >= 0 && res / 16 <= 9)
            tmp += (char) (res / 16 + '0');
        else
            tmp += (char) (res / 16 - 10 + 'A');

        if (res % 16 >= 0 && res % 16 <= 9)
            tmp += (char) (res % 16 + '0');
        else
            tmp += (char) (res % 16 - 10 + 'A');
        return tmp;

    }

    /**
     * 十六进制转十进制
     *
     * @param res
     * @return
     */
    public static char Hex2Dic(String res) {
        char tmp;
        if (res.charAt(0) >= '0' && res.charAt(0) <= '9')
            tmp = (char) ((res.charAt(0) - '0') * 16);
        else
            tmp = (char) ((res.charAt(0) - 'a' + 10) * 16);

        if (res.charAt(1) >= '0' && res.charAt(1) <= '9')
            tmp += (char) ((res.charAt(1) - '0'));
        else
            tmp += (char) ((res.charAt(1) - 'a' + 10));
        return tmp;
    }

    /**
     * 转换为16进制的字符串
     *
     * @param res
     * @return
     */
    public static String Dic2Hex(String res) {
        String tmp = "";
        for (int i = 0; i < res.length(); i++)//十进制转十六进制
        {
            int t = res.charAt(i);
            tmp += DecimalUtil.Dic2Hex(t);
            tmp += ' ';
        }
        return tmp;
    }

    public static int getInteger(String value) {
        if (TextUtils.isEmpty(value)) return 0;
        return Integer.valueOf(value);
    }
}
